package com.ruoyi.web.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Article;
import com.ruoyi.system.domain.Tags;
import com.ruoyi.system.service.IArticleService;

/**
 * 文章查询Controller
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/system/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;

    /**
     * 查询文章查询列表
     */
    @GetMapping("/listByTag")
    public TableDataInfo selectArticleListByTag(Tags tags)
    {
        startPage();
        List<Article> list = articleService.selectArticleListByTag(tags);
        return getDataTable(list);
    }
    
    /**
     * 查询文章查询列表
     */
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article)
    {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }
     
    /**
     * 查询文章查询列表（数据不分页）
     */
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/nplist")
    public AjaxResult nplist(Article article)
    {
        return success(articleService.selectArticleListde(article));
    }
    
    /**
     * 查询热门文章查询列表
     */
//    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/rmlist")
    public AjaxResult rmlist(Article article) {
        return success(articleService.selectArticleListrm(article));
    }
    
    /**
     * 导出文章查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "文章查询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章查询数据");
    }

    /**
     * 获取文章查询详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Integer articleId)
    {
        return success(articleService.selectArticleByArticleId(articleId));
    }

    /**
     * 新增文章查询
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "文章查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return success(articleService.insertArticle(article));
    }

    /**
     * 修改文章查询
     */
//    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "文章查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
//    	System.out.println(article);
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章查询
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "文章查询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Integer[] articleIds)
    {
        return toAjax(articleService.deleteArticleByArticleIds(articleIds));
    }
}
